package controllers

import (
	"ErgoGo/pkg/config"

	"github.com/gin-contrib/sessions"
	"github.com/gin-gonic/gin"
)

func LoginForm(c *gin.Context) {
	c.HTML(200, "login", gin.H{})
}

func Login(c *gin.Context) {
	username := c.PostForm("username")
	password := c.PostForm("password")

	if username == config.Get("app.admin_username") && password == config.Get("app.admin_password") {
		// 设置session
		// 设置会话
		session := sessions.Default(c)
		session.Set("authenticated", true)
		session.Save()
		c.Redirect(302, "/dashboard")
		return
	}

	c.HTML(200, "login", gin.H{
		"Error": "用户名或密码错误",
	})
}

func Logout(c *gin.Context) {
	// 清除会话
	session := sessions.Default(c)
	session.Clear()
	session.Save()
	c.Redirect(302, "/login")
}
